<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Node.js + GitHub Webhooks实现自动化部署 | 个人技术的分享</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/logo.ico">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/logo.png">
    <link rel="stylesheet" href="/styles/iconfont.css">
    <meta name="description" content="切图仔、CV/API工程师。">
    <link rel="preload" href="/assets/css/0.styles.bab1657c.css" as="style"><link rel="preload" href="/assets/js/app.dfe2ff49.js" as="script"><link rel="preload" href="/assets/js/3.bd810dc8.js" as="script"><link rel="preload" href="/assets/js/4.7eb6bf85.js" as="script"><link rel="preload" href="/assets/js/100.aff7ce2a.js" as="script"><link rel="prefetch" href="/assets/js/10.9314e6e2.js"><link rel="prefetch" href="/assets/js/101.6d8bb072.js"><link rel="prefetch" href="/assets/js/102.ab4c6afe.js"><link rel="prefetch" href="/assets/js/103.f613c5b8.js"><link rel="prefetch" href="/assets/js/104.3cd8f787.js"><link rel="prefetch" href="/assets/js/105.ddd67c67.js"><link rel="prefetch" href="/assets/js/106.bf26ab7e.js"><link rel="prefetch" href="/assets/js/107.2a6632ce.js"><link rel="prefetch" href="/assets/js/108.5885d4d6.js"><link rel="prefetch" href="/assets/js/109.22628ed2.js"><link rel="prefetch" href="/assets/js/11.67c23384.js"><link rel="prefetch" href="/assets/js/110.8dcf645b.js"><link rel="prefetch" href="/assets/js/111.a75b4e6d.js"><link rel="prefetch" href="/assets/js/112.762ffa1c.js"><link rel="prefetch" href="/assets/js/113.c5b41064.js"><link rel="prefetch" href="/assets/js/114.34ec817a.js"><link rel="prefetch" href="/assets/js/115.5f0aa55b.js"><link rel="prefetch" href="/assets/js/116.698defdd.js"><link rel="prefetch" href="/assets/js/117.bb7bcca3.js"><link rel="prefetch" href="/assets/js/118.2d66657c.js"><link rel="prefetch" href="/assets/js/119.8a262cdd.js"><link rel="prefetch" href="/assets/js/12.bb124c65.js"><link rel="prefetch" href="/assets/js/120.3c491dee.js"><link rel="prefetch" href="/assets/js/121.3505af80.js"><link rel="prefetch" href="/assets/js/122.04e9a7cf.js"><link rel="prefetch" href="/assets/js/123.4e17d778.js"><link rel="prefetch" href="/assets/js/124.13b04833.js"><link rel="prefetch" href="/assets/js/125.bd0012a2.js"><link rel="prefetch" href="/assets/js/126.99cc4ec3.js"><link rel="prefetch" href="/assets/js/127.b5a83745.js"><link rel="prefetch" href="/assets/js/128.56b40a89.js"><link rel="prefetch" href="/assets/js/129.1489ad27.js"><link rel="prefetch" href="/assets/js/13.1e4b99c7.js"><link rel="prefetch" href="/assets/js/130.c64a96a8.js"><link rel="prefetch" href="/assets/js/131.7dbeedc5.js"><link rel="prefetch" href="/assets/js/132.c11a3a90.js"><link rel="prefetch" href="/assets/js/133.c02b1035.js"><link rel="prefetch" href="/assets/js/134.27e97bf6.js"><link rel="prefetch" href="/assets/js/135.5cf1ffcb.js"><link rel="prefetch" href="/assets/js/136.732866b5.js"><link rel="prefetch" href="/assets/js/137.71150c2b.js"><link rel="prefetch" href="/assets/js/138.6edc5f97.js"><link rel="prefetch" href="/assets/js/139.7dc5785a.js"><link rel="prefetch" href="/assets/js/14.230d6df1.js"><link rel="prefetch" href="/assets/js/140.df409b41.js"><link rel="prefetch" href="/assets/js/141.23e4a662.js"><link rel="prefetch" href="/assets/js/15.fbe6b5b2.js"><link rel="prefetch" href="/assets/js/16.72f3903d.js"><link rel="prefetch" href="/assets/js/17.667ac210.js"><link rel="prefetch" href="/assets/js/18.9b60328e.js"><link rel="prefetch" href="/assets/js/19.d1bb7ffd.js"><link rel="prefetch" href="/assets/js/20.4bf72a89.js"><link rel="prefetch" href="/assets/js/21.d62b0bbb.js"><link rel="prefetch" href="/assets/js/22.c15b497e.js"><link rel="prefetch" href="/assets/js/23.583da9a1.js"><link rel="prefetch" href="/assets/js/24.59df73ca.js"><link rel="prefetch" href="/assets/js/25.ae681969.js"><link rel="prefetch" href="/assets/js/26.4652101c.js"><link rel="prefetch" href="/assets/js/27.4f2c4857.js"><link rel="prefetch" href="/assets/js/28.cd131e05.js"><link rel="prefetch" href="/assets/js/29.3d4a8ee1.js"><link rel="prefetch" href="/assets/js/30.bc6d42d1.js"><link rel="prefetch" href="/assets/js/31.2c405117.js"><link rel="prefetch" href="/assets/js/32.0ceecc6e.js"><link rel="prefetch" href="/assets/js/33.12a93636.js"><link rel="prefetch" href="/assets/js/34.f4994641.js"><link rel="prefetch" href="/assets/js/35.926aff5c.js"><link rel="prefetch" href="/assets/js/36.1850fac0.js"><link rel="prefetch" href="/assets/js/37.7990b07f.js"><link rel="prefetch" href="/assets/js/38.0c59d91b.js"><link rel="prefetch" href="/assets/js/39.f5ad73cc.js"><link rel="prefetch" href="/assets/js/40.9853630f.js"><link rel="prefetch" href="/assets/js/41.ee8f051d.js"><link rel="prefetch" href="/assets/js/42.03d761cf.js"><link rel="prefetch" href="/assets/js/43.6f6c7421.js"><link rel="prefetch" href="/assets/js/44.47e28062.js"><link rel="prefetch" href="/assets/js/45.af48f901.js"><link rel="prefetch" href="/assets/js/46.c2ef1108.js"><link rel="prefetch" href="/assets/js/47.3b754edd.js"><link rel="prefetch" href="/assets/js/48.41fd0e4b.js"><link rel="prefetch" href="/assets/js/49.fa8bb2d6.js"><link rel="prefetch" href="/assets/js/5.9554ffdf.js"><link rel="prefetch" href="/assets/js/50.470960b6.js"><link rel="prefetch" href="/assets/js/51.f641e205.js"><link rel="prefetch" href="/assets/js/52.292aa625.js"><link rel="prefetch" href="/assets/js/53.06feb6d0.js"><link rel="prefetch" href="/assets/js/54.bd22026a.js"><link rel="prefetch" href="/assets/js/55.ea51cd92.js"><link rel="prefetch" href="/assets/js/56.f909fe9f.js"><link rel="prefetch" href="/assets/js/57.02f3b546.js"><link rel="prefetch" href="/assets/js/58.70a7433b.js"><link rel="prefetch" href="/assets/js/59.c0fc6ad7.js"><link rel="prefetch" href="/assets/js/6.32562f11.js"><link rel="prefetch" href="/assets/js/60.6e2b4c40.js"><link rel="prefetch" href="/assets/js/61.a52a09ff.js"><link rel="prefetch" href="/assets/js/62.27ccbcf9.js"><link rel="prefetch" href="/assets/js/63.8a96fdd6.js"><link rel="prefetch" href="/assets/js/64.4c572d6a.js"><link rel="prefetch" href="/assets/js/65.b1d5b340.js"><link rel="prefetch" href="/assets/js/66.7107370e.js"><link rel="prefetch" href="/assets/js/67.666b4292.js"><link rel="prefetch" href="/assets/js/68.8c3bd9eb.js"><link rel="prefetch" href="/assets/js/69.4c5cd008.js"><link rel="prefetch" href="/assets/js/7.84c0fac9.js"><link rel="prefetch" href="/assets/js/70.c219a726.js"><link rel="prefetch" href="/assets/js/71.a952c191.js"><link rel="prefetch" href="/assets/js/72.b1635d94.js"><link rel="prefetch" href="/assets/js/73.c828f2de.js"><link rel="prefetch" href="/assets/js/74.29555c6a.js"><link rel="prefetch" href="/assets/js/75.ddd32435.js"><link rel="prefetch" href="/assets/js/76.33b11087.js"><link rel="prefetch" href="/assets/js/77.1992b55d.js"><link rel="prefetch" href="/assets/js/78.6e4af851.js"><link rel="prefetch" href="/assets/js/79.0c6e5357.js"><link rel="prefetch" href="/assets/js/8.b775bc4d.js"><link rel="prefetch" href="/assets/js/80.a2494e79.js"><link rel="prefetch" href="/assets/js/81.ba3634f7.js"><link rel="prefetch" href="/assets/js/82.bf7c34dc.js"><link rel="prefetch" href="/assets/js/83.a424781a.js"><link rel="prefetch" href="/assets/js/84.1024c1e9.js"><link rel="prefetch" href="/assets/js/85.75a55948.js"><link rel="prefetch" href="/assets/js/86.f957a19e.js"><link rel="prefetch" href="/assets/js/87.0d718ad6.js"><link rel="prefetch" href="/assets/js/88.f17614e7.js"><link rel="prefetch" href="/assets/js/89.354fe35d.js"><link rel="prefetch" href="/assets/js/9.8eda8208.js"><link rel="prefetch" href="/assets/js/90.6dd96ee4.js"><link rel="prefetch" href="/assets/js/91.136af10e.js"><link rel="prefetch" href="/assets/js/92.a9c072ce.js"><link rel="prefetch" href="/assets/js/93.c52cc17a.js"><link rel="prefetch" href="/assets/js/94.03f26b32.js"><link rel="prefetch" href="/assets/js/95.2d937101.js"><link rel="prefetch" href="/assets/js/96.b93eee83.js"><link rel="prefetch" href="/assets/js/97.075388e9.js"><link rel="prefetch" href="/assets/js/98.297b61d9.js"><link rel="prefetch" href="/assets/js/99.df97102a.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.2e6ea98f.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bab1657c.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="图标: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active home-link"><img src="/logo.png" alt="个人技术的分享" class="logo"> <span class="site-name">个人技术的分享</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          博文
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/blog/leetcode/">
          LeetCode
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/thoughts/">
          随想
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <!----></nav></div></div> <!----></header> <aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/blog/other/" aria-current="page" title="快速导航" class="sidebar-link">快速导航</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>系统环境相关</span> <span class="arrow right"><i aria-label="图标: down" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></i></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发工具相关</span> <span class="arrow right"><i aria-label="图标: down" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></i></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>部署相关</span> <span class="arrow down"><i aria-label="图标: down" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></i></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/other/aboutdeploy/sub-domain-deploy.html" title="Nginx二级域名部署实践" class="sidebar-link">Nginx二级域名部署实践</a></li><li><a href="/blog/other/aboutdeploy/sub-directory-deploy.html" title="Nginx部署React项目至服务器二级目录实践" class="sidebar-link">Nginx部署React项目至服务器二级目录实践</a></li><li><a href="/blog/other/aboutdeploy/nginx-proxy-resource.html" title="使用Nginx代理访问图片等资源" class="sidebar-link">使用Nginx代理访问图片等资源</a></li><li><a href="/blog/other/aboutdeploy/nginx-config-https.html" title="Nginx配置HTTPS/安装SSL证书" class="sidebar-link">Nginx配置HTTPS/安装SSL证书</a></li><li><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html" aria-current="page" title="Node.js + GitHub Webhooks实现自动化部署" class="active sidebar-link">Node.js + GitHub Webhooks实现自动化部署</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html#一、github-webhooks" title="一、GitHub WebHooks" class="sidebar-link">一、GitHub WebHooks</a></li><li class="sidebar-sub-header"><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html#二、使用node-js搭建服务" title="二、使用node.js搭建服务" class="sidebar-link">二、使用node.js搭建服务</a></li><li class="sidebar-sub-header"><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html#三、将服务部署至服务器" title="三、将服务部署至服务器" class="sidebar-link">三、将服务部署至服务器</a></li><li class="sidebar-sub-header"><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html#四、遇到的问题" title="四、遇到的问题" class="sidebar-link">四、遇到的问题</a></li><li class="sidebar-sub-header"><a href="/blog/other/aboutdeploy/auto-deploy-nodejs.html#五、参考资料" title="五、参考资料" class="sidebar-link">五、参考资料</a></li></ul></li><li><a href="/blog/other/aboutdeploy/nginx-deploy-for-centos.html" title="Nginx部署笔记-CentOS" class="sidebar-link">Nginx部署笔记-CentOS</a></li><li><a href="/blog/other/aboutdeploy/nginx-error-summary-2.html" title="Nginx部署笔记-常见错误总结（2）" class="sidebar-link">Nginx部署笔记-常见错误总结（2）</a></li><li><a href="/blog/other/aboutdeploy/nginx-error-summary-1.html" title="Nginx部署笔记-常见错误总结（1）" class="sidebar-link">Nginx部署笔记-常见错误总结（1）</a></li><li><a href="/blog/other/aboutdeploy/nginx-deploy-summary.html" title="Nginx部署笔记-常用配置总结" class="sidebar-link">Nginx部署笔记-常用配置总结</a></li><li><a href="/blog/other/aboutdeploy/nginx-start.html" title="Nginx部署笔记-Windows（2）开机自启动" class="sidebar-link">Nginx部署笔记-Windows（2）开机自启动</a></li><li><a href="/blog/other/aboutdeploy/nginx-deploy-record.html" title="Nginx部署笔记-Windows（1）" class="sidebar-link">Nginx部署笔记-Windows（1）</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>博客相关</span> <span class="arrow right"><i aria-label="图标: down" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></i></span></p> <!----></section></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h1 id="node-js-github-webhooks实现自动化部署"><a href="#node-js-github-webhooks实现自动化部署" class="header-anchor">#</a> Node.js + GitHub Webhooks实现自动化部署</h1> <!----> <blockquote><p><strong>博客</strong>已经成功部署到云服务器了，但是还有一个问题需要解决。</p> <p>👉博客的文章增加、修改等都在个人PC上完成，并且本地有开发环境，<strong>编译之后生成的适合生产环境的项目源码会提交到GitHub上的远程仓库</strong>。此时博客已迁移到了云服务器上，为了使云服务器上的代码的版本保持最新，我有想到<strong>三个操作</strong>：👇</p> <ul><li>1、远程登录云服务器手动更新；</li> <li>2、为<strong>服务器设置定时脚本</strong>，比如每隔24小时执行一次，与GitHub上的仓库同步；</li> <li>3、建立<strong>某种自动更新机制</strong>，代码更新到GitHub仓库之后让云服务器这边触发同步。</li></ul> <p>第一个操作明显繁琐，不符合懒人需求🤣，第二个操作<strong>定时脚本</strong>可以用用，但明显第三个才是出路啊。。</p></blockquote> <h2 id="一、github-webhooks"><a href="#一、github-webhooks" class="header-anchor">#</a> 一、GitHub WebHooks</h2> <p><code>GitHub Webhooks</code>是GitHub提供的一个API，如果为一个GitHub上的一个远程仓库配置了<code>Webhooks</code>，当有人往这个远程仓库<code>push</code>代码完成后，GitHub会往<code>Webhooks</code>里配置的地址发送一个<code>POST</code>请求，并在<code>body</code>中给出本次更新的仓库信息、更新的文件等信息。</p> <p>这个<code>post</code>请求相当于一个<strong>通知</strong>，我们收到<code>post</code>请求后可以对<strong>服务器端的本地仓库进行更新</strong>，拉取·个 GitHub上的最新代码，从而实现<strong>一次提交，服务器端自动化部署</strong>。</p> <details class="custom-block details"><summary>展开查看详情</summary> <p>配置<code>GitHub Webhooks</code>：进入一个仓库 -&gt; 右上角<code>Settings</code> -&gt; 侧边栏<code>Webhooks</code>。</p> <p><img src="/images/other/aboutdeploy/auto-deploy-01.png" alt="auto-deploy-01"></p> <p>一次<code>push</code>时<code>Webhooks</code>返回的部分数据：👇</p> <p><img src="/images/other/aboutdeploy/auto-deploy-02.png" alt="auto-deploy-02"></p></details> <h2 id="二、使用node-js搭建服务"><a href="#二、使用node-js搭建服务" class="header-anchor">#</a> 二、使用<code>node.js</code>搭建服务</h2> <p>说实话这是第一次正经使用<code>node.js</code>。。</p> <blockquote><p>以下为一个简单的<code>POST</code>请求实例代码。</p> <p><a href="https://github.com/StarlightUnion/AutoUpdateServer-Node.js" target="_blank" rel="noopener noreferrer">完整的项目源码在此<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> ，<strong>引入了日志功能，可以记录服务的常规和异常信息</strong>。</p></blockquote> <h3 id="_1-写个demo测试一下"><a href="#_1-写个demo测试一下" class="header-anchor">#</a> 1.写个DEMO测试一下</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token constant">HOST</span><span class="token operator">:</span> <span class="token string">&quot;127.0.0.1&quot;</span><span class="token punctuation">,</span>
  <span class="token constant">PORT</span><span class="token operator">:</span> <span class="token number">9000</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// server.js</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> bodyParser <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'body-parser'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> cors <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'cors'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 跨域</span>
<span class="token keyword">const</span> server <span class="token operator">=</span> <span class="token function">express</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> config <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./config'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 配置 config.js</span>
<span class="token keyword">const</span> <span class="token constant">HOST</span> <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token constant">HOST</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">PORT</span> <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token constant">PORT</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>bodyParser<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>bodyParser<span class="token punctuation">.</span><span class="token function">urlencoded</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">extended</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token function">cors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/api/update'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">request<span class="token punctuation">,</span> response</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
  response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">host</span><span class="token operator">:</span> <span class="token constant">HOST</span><span class="token punctuation">,</span>
  <span class="token literal-property property">port</span><span class="token operator">:</span> <span class="token constant">PORT</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Server is running in http://</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">PORT</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><p>安装必要依赖后执行<code>node server.js</code>，在<strong>Postman中模拟一次请求</strong>，如下，成功收到响应！</p> <p><img src="/images/other/aboutdeploy/auto-deploy-03.png" alt="auto-deploy-03"></p> <h3 id="_2-接着完善一下"><a href="#_2-接着完善一下" class="header-anchor">#</a> 2.接着完善一下</h3> <blockquote><p>主要增加了：</p> <ul><li>将命令整合至<code>update.sh</code>脚本，并通过<code>node.js</code>相关API调用；</li> <li><strong>从<code>body</code>中获取更新的仓库名</strong>并将其作为一个<strong>参数</strong>传入脚本，这样服务可以应用于多个仓库。</li></ul></blockquote> <details class="custom-block details"><summary>展开查看源码</summary> <h4 id="项目文件结构"><a href="#项目文件结构" class="header-anchor">#</a> 项目文件结构</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>.node-update
├── static
│   ├── server.js
│   ├── config.js
│   └── update.sh
└── package.json
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="server-js服务本体"><a href="#server-js服务本体" class="header-anchor">#</a> <code>server.js</code>服务本体</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// server.js</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> bodyParser <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'body-parser'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> cors <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'cors'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 跨域</span>
<span class="token keyword">const</span> server <span class="token operator">=</span> <span class="token function">express</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> spawn <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'child_process'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>spawn<span class="token punctuation">;</span>

<span class="token keyword">const</span> config <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./config'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 配置 config.js</span>
<span class="token keyword">const</span> <span class="token constant">HOST</span> <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token constant">HOST</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">PORT</span> <span class="token operator">=</span> config<span class="token punctuation">.</span><span class="token constant">PORT</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>bodyParser<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>bodyParser<span class="token punctuation">.</span><span class="token function">urlencoded</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">extended</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
server<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token function">cors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// github webhooks 需选用 x-www-form-urlencoded</span>
server<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/api/update'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">request<span class="token punctuation">,</span> response</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// console.log(request.body);</span>
    <span class="token keyword">const</span> repoName <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>body<span class="token punctuation">.</span>payload<span class="token punctuation">)</span><span class="token punctuation">.</span>repository<span class="token punctuation">.</span>name
    <span class="token keyword">const</span> cmd <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/documents/GitHub/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>repoName<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span><span class="token comment">// 本地仓库地址</span>

    <span class="token keyword">let</span> res <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
    <span class="token comment">// 调用命令行，执行命令(sh update.sh)，并给update.sh传递一个参数cmd</span>
    <span class="token keyword">const</span> process <span class="token operator">=</span> <span class="token function">spawn</span><span class="token punctuation">(</span><span class="token string">'sh'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'update.sh'</span><span class="token punctuation">,</span> cmd<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    process<span class="token punctuation">.</span>stdout<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> ds <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// console.log(ds);</span>
      res <span class="token operator">+=</span> ds<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    process<span class="token punctuation">.</span>stderr<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> ds <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// console.log(ds);</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// response.json(request.body);</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">host</span><span class="token operator">:</span> <span class="token constant">HOST</span><span class="token punctuation">,</span>
  <span class="token literal-property property">port</span><span class="token operator">:</span> <span class="token constant">PORT</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Server is running in http://</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">HOST</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">PORT</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br></div></div><h4 id="update-sh更新脚本"><a href="#update-sh更新脚本" class="header-anchor">#</a> <code>update.sh</code>更新脚本</h4> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># update.sh</span>
<span class="token builtin class-name">echo</span> <span class="token string">'start update...'</span>
<span class="token comment"># echo $1</span>
<span class="token builtin class-name">cd</span> <span class="token variable">$1</span> <span class="token comment"># 接收传入的参数 即地址</span>

<span class="token comment"># 拉取更新</span>
<span class="token function">git</span> fetch <span class="token parameter variable">--all</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> origin/master

<span class="token comment"># 获取最新代码合并到本地</span>
<span class="token comment"># git pull origin</span>

<span class="token builtin class-name">echo</span> <span class="token string">'update complete!'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="package-json"><a href="#package-json" class="header-anchor">#</a> <code>package.json</code></h4> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;node-update&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;1.0.0&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;main&quot;</span><span class="token operator">:</span> <span class="token string">&quot;server.js&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;server&quot;</span><span class="token operator">:</span> <span class="token string">&quot;cd static &amp;&amp; nodemon server.js&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;license&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ISC&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;dependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;body-parser&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^1.19.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;cors&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^2.8.5&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;express&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^4.17.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;nodemon&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^1.11.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;winston&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^3.2.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;winston-daily-rotate-file&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^4.4.2&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div></details> <h2 id="三、将服务部署至服务器"><a href="#三、将服务部署至服务器" class="header-anchor">#</a> 三、将服务部署至服务器</h2> <h3 id="_1-运行前的配置"><a href="#_1-运行前的配置" class="header-anchor">#</a> 1.运行前的配置</h3> <blockquote><ul><li>1.设置<code>config.js</code>👇，<code>HOST</code>为服务的IP，<code>PORT</code>填写服务的端口。</li></ul> <div class="custom-block danger"><p class="custom-block-title">注意事项</p> <ul><li>需要<strong>确保填写的端口正确开通并暴露在公网</strong>，需设置防火墙（Windows）或安全组（云服务器）；</li> <li>云服务器的话<code>HOST</code>需要<strong>填写服务器的内网地址</strong>，填写公网地址将会报错；</li> <li>服务器需安装<code>node.js</code>环境，参考<a href="/blog/other/systemenv/nodejs-config-for-centos.html">CentOS下配置Node.js和Nginx环境</a></li></ul></div> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">...</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
 <span class="token constant">HOST</span><span class="token operator">:</span> <span class="token string">&quot;127.0.0.1&quot;</span><span class="token punctuation">,</span><span class="token comment">// 云服务器要填写该服务器的内网IP</span>
 <span class="token constant">PORT</span><span class="token operator">:</span> <span class="token number">9000</span><span class="token punctuation">,</span>
 <span class="token literal-property property">logger</span><span class="token operator">:</span> logger<span class="token comment">// 日志</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token operator">...</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>2.设置<code>server.js</code></li></ul> <p>这里需要配置服务器上的<strong>本地仓库的绝对地址</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">...</span>
<span class="token keyword">const</span> repoName <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>body<span class="token punctuation">.</span>payload<span class="token punctuation">)</span><span class="token punctuation">.</span>repository<span class="token punctuation">.</span>name
<span class="token keyword">const</span> cmd <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">/documents/GitHub/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>repoName<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span><span class="token comment">// 本地仓库地址</span>
<span class="token operator">...</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></blockquote> <p>将文件传至服务器，在命令行中进入该目录，执行下面命令。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> <span class="token comment"># 安装依赖</span>
<span class="token function">npm</span> run server <span class="token comment"># 运行服务</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="/images/other/aboutdeploy/auto-deploy-07.png" alt="auto-deploy-07"></p> <p>一次<code>POST</code>请求打印出的信息👇</p> <p><img src="/images/other/aboutdeploy/auto-deploy-08.png" alt="auto-deploy-08"></p> <h3 id="_2-实现服务端常驻"><a href="#_2-实现服务端常驻" class="header-anchor">#</a> 2.实现服务端常驻</h3> <blockquote><p><code>npm run server</code>只能实现服务的开发环境下的调试部署，要想实现服务的常驻，需要安装<code>pm2</code>。</p></blockquote> <p>运行<code>npm i pm2 -g</code>全局安装<code>pm2</code>，下面第一图中可以看到<strong>安装的路径</strong>。</p> <p><img src="/images/other/aboutdeploy/auto-deploy-04.png" alt="auto-deploy-04"></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>pm2 <span class="token comment"># 尝试调用，发现没有用</span>

<span class="token comment"># 将安装路径中的pm2建立软链接到全局路径，将pm2设置为全局命令</span>
<span class="token function">ln</span> <span class="token parameter variable">-s</span> /root/node-v14.3.0-linux-x64/bin/pm2 /usr/bin/pm2
pm2 <span class="token comment"># 成功</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="/images/other/aboutdeploy/auto-deploy-05.png" alt="auto-deploy-05"></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token builtin class-name">cd</span> static <span class="token comment"># 进入static目录</span>
pm2 start server.js <span class="token comment"># 运行server.js并常驻</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="/images/other/aboutdeploy/auto-deploy-06.png" alt="auto-deploy-06"></p> <h3 id="_3-pm2常用命令"><a href="#_3-pm2常用命令" class="header-anchor">#</a> 3.<code>pm2</code>常用命令</h3> <ul><li>查看当前使用<code>pm2</code>常驻的服务：<code>pm2 list</code></li> <li>启动服务：<code>pm2 start example.js</code></li> <li>重启服务：<code>pm2 restart example.js</code></li> <li>停止服务：<code>pm2 stop all</code>/<code>pm2 stop 0</code>/<code>pm2 stop example.js</code></li> <li>查看编号0的服务：<code>pm2 show 0</code></li> <li>监控服务：<code>pm2 monit</code>/<code>pm2 monit 0</code>/<code>pm2 monit example.js</code></li> <li>打印日志：<code>pm2 logs</code>/<code>pm2 logs 0</code>/<code>pm2 logs example.js</code></li></ul> <h2 id="四、遇到的问题"><a href="#四、遇到的问题" class="header-anchor">#</a> 四、遇到的问题</h2> <h3 id="拉取远程仓库代码失败"><a href="#拉取远程仓库代码失败" class="header-anchor">#</a> 拉取远程仓库代码失败</h3> <blockquote><p>错误提示：<code>error: PRC failed; result=18, HTTP code = 200</code>、<code>fatal: The remote end hung up unexpectedly</code>、<code>fatal: early EOF</code>、<code>fatal: index-pack failed</code>、<code>error: could not fetch origin</code>。</p></blockquote> <p>搜索一番，需要修改<code>Git</code>的传输字节大小设置。</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">git</span> config <span class="token parameter variable">--list</span> <span class="token comment"># 查看 http.postbuffer</span>

<span class="token comment"># 修改 http.postbuffer 为 1024288000</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> http.postBuffer <span class="token number">1024288000</span>
<span class="token function">git</span> config <span class="token parameter variable">--list</span> <span class="token comment"># 再次查看</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="/images/other/aboutdeploy/auto-deploy-09.png" alt="auto-deploy-09"></p> <p><img src="/images/other/aboutdeploy/auto-deploy-10.png" alt="auto-deploy-10"></p> <h2 id="五、参考资料"><a href="#五、参考资料" class="header-anchor">#</a> 五、参考资料</h2> <ul><li>1.<a href="https://www.jianshu.com/p/e709b71f12da" target="_blank" rel="noopener noreferrer">pm2常用命令<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">2 years ago</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blog/other/aboutdeploy/nginx-config-https.html" class="prev"><i aria-label="图标: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        Nginx配置HTTPS/安装SSL证书
      </a></span> <span class="next"><a href="/blog/other/aboutdeploy/nginx-deploy-for-centos.html">
        Nginx部署笔记-CentOS
        <i aria-label="图标: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><div id="live2d-widget" class="live2d-widget-container" style="position:fixed;right:10px;bottom:60px;width:135px;height:300px;z-index:99999;opacity:0.8;pointer-events:none;"><canvas id="live2d_canvas" width="135" height="300" class="live2d_canvas" style="position:absolute;left:0px;top:0px;width:135px;height:300px;"></canvas></div></div></div>
    <script src="/assets/js/app.dfe2ff49.js" defer></script><script src="/assets/js/3.bd810dc8.js" defer></script><script src="/assets/js/4.7eb6bf85.js" defer></script><script src="/assets/js/100.aff7ce2a.js" defer></script>
  </body>
</html>